﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OpenXML.BLL
{
    public class WordFile : WordprocessingDocument
    {
        private string name { get; set; }
        private string path { get; set; }

        public static void CreateWordDocument(string filepath, string sample, int id)
        {
            // Create a document by supplying the filepath
            using (WordprocessingDocument wordDocument = 
                WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
            {
                // Add a main document part
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                // Create the document structure and add some text
                mainPart.Document = new Document();

                Body body = mainPart.Document.AppendChild(new Body());
                BookmarkStart bkmStart = body.AppendChild(new BookmarkStart() 
                { 
                    Name = "bkmStart" + id.ToString(), Id = "s" + id.ToString() 
                });
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());
                Text text = run.AppendChild(new Text(sample));
                BookmarkEnd bkmEnd = body.AppendChild(new BookmarkEnd() 
                { 
                    Id = "s" + id.ToString() 
                });
            }
        }

        public static void OpenWordDocument(string filepath)
        {
            WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true);
        }

        public static void CloseOpenWordDocument(WordprocessingDocument wd)
        {
            wd.Close();
        }

        public static void GotoBookmark()
        { }

        public static void ReplaceTextToSpecificLocation(string filepath, string text, string motif)
        {

        }

        public static void AddTextToSpecificBookmark(string filepath, string id, string sample)
        {
            using (WordprocessingDocument document = WordprocessingDocument.Open(filepath, true))
            {
                var mainPart = document.MainDocumentPart;
                var res = from bm in mainPart.Document.Body.Descendants<BookmarkStart>()
                          where bm.Name == id
                          select bm;

                var bookmark = res.SingleOrDefault();
                if (bookmark != null)
                {
                    var parent = bookmark.NextSibling();

                    Text text = new Text(sample);
                    Run run = new Run(new RunProperties(new Bold()));
                    run.Append(text);
                    Paragraph newParagraph = new Paragraph(run);

                    parent.InsertAfterSelf(newParagraph);
                }
            }
        }

        public static void AddTextToWordDocument(string filepath, string txt, int id)
        {
            // Open a WordprocessingDocument for editing using the filepath
            WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true);

            // Assign a reference to the existing document body
            Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

            // Add Bookmark Start
            BookmarkStart bkmStart = body.AppendChild(new BookmarkStart() 
            { 
                Name = "bkmStart" + id.ToString(), Id = "s" + id.ToString() 
            });

            // Add new text
            Paragraph para = body.AppendChild(new Paragraph());
            Run run = para.AppendChild(new Run());
            run.AppendChild(new Text(txt));

            // Add Bookmark End
            BookmarkEnd bkmEnd = body.AppendChild(new BookmarkEnd() 
            { 
                Id = "s" + id.ToString() 
            });

            // Close the handle explicitly.
            wordprocessingDocument.Close();
        }

        public static void AddFontStyle(StyleDefinitionsPart styleDefinitionsPart, string styleid, string stylename, string aliases = "")
        {
            // Get access to the root element of the styles part
            Styles styles = styleDefinitionsPart.Styles;

            // Create a new character style and specify some of the attributes
            DocumentFormat.OpenXml.Wordprocessing.Style style = new DocumentFormat.OpenXml.Wordprocessing.Style()
            {
                Type = StyleValues.Character,
                StyleId = styleid,
                CustomStyle = true
            };

            // Create and add the child elements (properties of the style).
            Aliases aliases1 = new Aliases() { Val = aliases };
            StyleName styleName1 = new StyleName() { Val = stylename };
            LinkedStyle linkedStyle1 = new LinkedStyle() { Val = "MyFirstStylePara" };
            if (aliases != "")
                style.Append(aliases1);
            style.Append(styleName1);
            style.Append(linkedStyle1);

            // Create the StyleRunProperties object and specify some of the run properties.
            StyleRunProperties styleRunProperties1 = new StyleRunProperties();
            Bold bold1 = new Bold();
            DocumentFormat.OpenXml.Wordprocessing.Color color1 = new DocumentFormat.OpenXml.Wordprocessing.Color() { ThemeColor = ThemeColorValues.Accent2 };
            RunFonts font1 = new RunFonts() { Ascii = "Tahoma" };
            Italic italic1 = new Italic();
            // Specify a 24 point size.
            DocumentFormat.OpenXml.Wordprocessing.FontSize fontSize1 = new DocumentFormat.OpenXml.Wordprocessing.FontSize() { Val = "48" };
            styleRunProperties1.Append(font1);
            styleRunProperties1.Append(fontSize1);
            styleRunProperties1.Append(color1);
            styleRunProperties1.Append(bold1);
            styleRunProperties1.Append(italic1);

            // Add the run properties to the style.
            style.Append(styleRunProperties1);

            // Add the style to the styles part.
            styles.Append(style);
        }

        public static StyleDefinitionsPart AddStylesPartToPackage(WordprocessingDocument doc)
        {
            StyleDefinitionsPart part;
            part = doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
            Styles root = new Styles();
            root.Save(part);
            return part;
        }

        public static bool ContainsBookmark(string filepath)
        {
            bool rep = true;

            return rep;
        }

        public static void SaveWordDocumentAs(WordprocessingDocument wd, string filepath)
        {

        }

        public static void SaveWordDocumentAsPdf(WordprocessingDocument wd, string filepath)
        {

        }
    }
}